﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FileEncodingConvertor
{
    public partial class MainForm : Form
    {
        string[] include_file_extensions = new string[0];
        string[] exclude_folder_names = new string[0];

        public MainForm()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            string[] drives = Environment.GetLogicalDrives();
            foreach (string drive in drives)
            {
                treeViewFolders.Nodes.Add(new TreeNode(drive, 0, 0));
            }
            LoadFolderNames();
            LoadFileExtensions();
        }

        private void treeViewFolders_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            AddSubnodes(e.Node, false);
            e.Node.Expand();
        }

        private void AddSubnodes(TreeNode node, bool fullRecursive)
        {
            if (node.Nodes.Count > 0 || node.ImageIndex == 2)
                return;

            // load nodes
            DirectoryInfo di = new DirectoryInfo(node.FullPath);
            foreach (var dir in di.GetDirectories())
            {
                TreeNode n = new TreeNode(dir.Name + "\\", 1, 1);
                node.Nodes.Add(n);
                if (fullRecursive && IsAutoCheckable(n))
                    AddSubnodes(n, true);
            }

            foreach (var file in di.GetFiles())
            {
                node.Nodes.Add(new TreeNode(file.Name, 2, 2));
            }
        }

        private void treeViewFolders_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Checked)
            {
                // load all subnodes
                AddSubnodes(e.Node, true);
                e.Node.ExpandAll();
            }
            else
            {
                e.Node.Collapse();
            }

            CheckAllSubNodes(e.Node);
        }

        private void CheckAllSubNodes(TreeNode treeNode)
        {
            foreach (TreeNode n in treeNode.Nodes)
            {
                // if it is folder
                if (IsAutoCheckable(n))
                    n.Checked = treeNode.Checked;
                CheckAllSubNodes(n);
            }
        }

        private bool IsAutoCheckable(TreeNode n)
        {
            // if it is folder
            if (n.ImageIndex != 2)
            {
                return !ArrayContains(n.Text.ToLower(), exclude_folder_names);
            }

            // if it is file we check if the extension is allowed
            return ArrayContains(n.Text.ToLower(), include_file_extensions);
        }

        private bool ArrayContains(string search, string[] values)
        {
            bool found = false;
            foreach (string item in exclude_folder_names)
            {
                if (item == search)
                {
                    found = true;
                    break;
                }
            }
            return found;
        }

        private void textBoxFiles_TextChanged(object sender, EventArgs e)
        {
            LoadFileExtensions();
        }

        private void LoadFileExtensions()
        {
            List<string> extensions = new List<string>();
            foreach (var item in textBoxFiles.Text.Split(','))
            {
                extensions.Add("." + item.ToLower().Trim(". ".ToCharArray()));
            }
            this.include_file_extensions = extensions.ToArray();
        }

        private void textBoxFolders_TextChanged(object sender, EventArgs e)
        {
            LoadFolderNames();
        }

        private void LoadFolderNames()
        {
            List<string> names = new List<string>();
            foreach (var item in textBoxFolders.Text.Split(','))
            {
                names.Add(item.ToLower().Trim(" \\".ToCharArray()) + "\\");
            }
            this.exclude_folder_names = names.ToArray();
        }

        private void buttonConvert_Click(object sender, EventArgs e)
        {
            string[] files = GetCheckedFiles(treeViewFolders.Nodes);
            PreviewForm pf = new PreviewForm(files);
            pf.ShowDialog(this);
        }

        private string[] GetCheckedFiles(TreeNodeCollection treeNodeCollection)
        {
            List<string> files = new List<string>();
            foreach (TreeNode item in treeNodeCollection)
            {
                if (item.ImageIndex != 2)
                {
                    files.AddRange(GetCheckedFiles(item.Nodes));
                }
                else
                {
                    if (item.Checked)
                        files.Add(item.FullPath);
                }
            }
            return files.ToArray();
        }
    }
}
